package com.ys.servers.migrate.verticle;

import com.ys.servers.common.verticle.BasicRestfulVerticle;
import com.ys.servers.migrate.service.MigrateTestService;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;

/**
 * Created by taotao on 2017/3/7.
 */
public class MigrateTestVerticle extends BasicRestfulVerticle {

    public static final String SERVICE_NAME = "migrate-test-api";
    private MigrateTestService migrateTestService;


    private static final String ORG_ADD = "/orgs";
    private static final String ORG_RETRIEVE = "/orgs/:id";
    private static final String ORG_RETRIEVE_ALL = "/orgs";
    private static final String ORG_UPDATE = "/orgs/:id";
    private static final String ORG_DELETE = "/orgs/:id";


    @Override
    public void start(Future<Void> future) throws Exception {
        super.start();
        final Router router = Router.router(vertx);
        // body handler
        router.route().handler(BodyHandler.create());
        // api route handler
        router.post(ORG_ADD).handler(this::addOrg);
        router.get(ORG_RETRIEVE).handler(this::retrieveOrg);
        router.get(ORG_RETRIEVE_ALL).handler(this::retrieveAllOrgs);
        router.patch(ORG_UPDATE).handler(this::updateOrg);
        router.delete(ORG_DELETE).handler(this::deleteOrg);

        String host = config().getString("user.account.http.address", "0.0.0.0");
        int port = config().getInteger("user.account.http.port", 8081);

        // create HTTP server and publish REST service
        createHttpServer(router, host, port)
                .compose(serverCreated -> publishHttpEndpoint(SERVICE_NAME, host, port))
                .setHandler(future.completer());
    }

    private void addOrg(RoutingContext context) {
        context.getBodyAsJson();
        System.out.println("Entry " + SERVICE_NAME + " :  " + "addOrg .");
    }

    private void retrieveOrg(RoutingContext context) {
        System.out.println("Entry " + SERVICE_NAME + " :  " + "retrieveOrg .");
    }

    private void retrieveAllOrgs(RoutingContext context) {
        JsonObject obj=context.getBodyAsJson();
        System.out.println("para1 :" +obj.getString("para1"));
        System.out.println("para2 :" +obj.getString("para2"));
        System.out.println("Entry " + SERVICE_NAME + " :  " + "retrieveAllOrgs .");
        String result = new JsonObject().put("message", "product_added").toString();
        context.response().setStatusCode(201)
                .putHeader("content-type", "application/json")
                .end(result);
    }


    private void updateOrg(RoutingContext context) {
        System.out.println("Entry " + SERVICE_NAME + " :  " + "updateOrg .");
    }

    private void deleteOrg(RoutingContext context) {
        System.out.println("Entry " + SERVICE_NAME + " :  " + "deleteOrg .");
    }

    @Override
    protected String interName() {
        return null;
    }

    @Override
    protected void initInternalDispatcher() {

    }
}
